package com.richapp.launcher.utils;

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * Created by yif on 16/8/2.
 */
public class HttpUtil {
    private static final String URL_UPDATE_ADVS = "";

    public static String get(String url) {
        String result = null;
        try {
            URL url2 = new URL(url);
            HttpURLConnection urlConnection = (HttpURLConnection) url2.openConnection();
            urlConnection.setConnectTimeout(10 * 1000);
            urlConnection.setReadTimeout(100 * 1000);
            try {
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                result = new String(readStream(in));
            } finally {
                urlConnection.disconnect();
            }
        } catch (Exception e) {
            Log.e(HttpUtil.class.getName(), "", e);
            return null;
        }
        return result;
    }

    public static String post(String url, Map<String, String> params) {
        String result = null;

        return result;
    }

    public static byte[] readStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        inStream.close();
        return outSteam.toByteArray();
    }
}
